﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace template_dll
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        public SqlConnection sqlconn = new SqlConnection();
        public int pharmcode = 0;
        public int enter = 0;
        public string pharmname = "";
        public DataTable datatable = new DataTable();
        private DataTable dt_ph = new DataTable();

        private void Form4_Load(object sender, EventArgs e)
        {
            datatable.Load(SPSel("pas_PharmViewSel", sqlconn));
            comboBox1 = AddItems(comboBox1, datatable, "Аптека сокр", pharmname);
            textBox1.Text = Convert.ToString(enter);           
        }

        private ComboBox AddItems(ComboBox cb, DataTable dt, string columnname, string pos)
        {
            foreach (DataRow dr in dt.Rows)
            {
                cb.Items.Add(Convert.ToString(dr[columnname]));
                if (Convert.ToString(dr[columnname]) == pos) cb.SelectedIndex = cb.Items.Count - 1;
            }
            return cb;
        }

        private SqlDataReader SPSel(string spname, SqlConnection con)
        {
            SqlCommand mycomm = con.CreateCommand();
            mycomm.CommandType = CommandType.StoredProcedure;
            mycomm.CommandText = spname;
            mycomm.CommandTimeout = 30;
            if (con.State == ConnectionState.Closed) con.Open();
            return mycomm.ExecuteReader();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text != "")
            {
                if (textBox1.Text != "" && textBox1.Text != "0")
                {
                    SqlCommand spins = sqlconn.CreateCommand();
                    spins.CommandType = CommandType.StoredProcedure;
                    spins.CommandText = "CountDW..pas_PharmEnterInsUpd";
                    spins.CommandTimeout = 30;

                    spins = AddParam(spins, DbType.Int32, "@phcode_new", SearchID(datatable, "Аптека сокр", comboBox1.Text, "Код"));
                    spins = AddParam(spins, DbType.Int32, "@phcode_old", pharmcode);
                    spins = AddParam(spins, DbType.Int32, "@enter_new", Convert.ToInt32(textBox1.Text));
                    spins = AddParam(spins, DbType.Int32, "@enter_old", enter);
                    if (sqlconn.State == ConnectionState.Closed) sqlconn.Open();
                    spins.ExecuteNonQuery();
                    Close();
                }
                else
                {
                    MessageBox.Show("Введите ид счетчика");
                    textBox1.Select();
                }
            }
            else 
            {
                MessageBox.Show("Выберите аптеку");
                comboBox1.Select();
            }

        }

        private SqlCommand AddParam(SqlCommand c, DbType dt, string param, object val)
        {
            c.Parameters.AddWithValue(param, val);
            //c.Parameters[param].Value = val;
            return c;
        }

        private object SearchID(DataTable dt, string columnname, string val, string col_id)
        {
            foreach (DataRow dr in dt.Rows)
                if (Convert.ToString(dr[columnname]) == val) return Convert.ToInt32(dr[col_id]);

            return DBNull.Value;
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsDigit(e.KeyChar)))
            {
                if (e.KeyChar != (char)Keys.Back)
                {
                    e.Handled = true;
                }
            }
        }
    }
}
